package com.afinal.intelligencetrash.ui.activity;


import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatTextView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;


import com.afinal.intelligencetrash.Presenter.Interinfaces.ActionCallBack;
import com.afinal.intelligencetrash.Presenter.LoginServiceImp;
import com.afinal.intelligencetrash.R;
import com.afinal.intelligencetrash.dagger.module.ActivityModule;
import com.afinal.intelligencetrash.global.CustomApplication;
import com.afinal.intelligencetrash.ui.base.AbstractBaseToolbarCoreActivity;
import com.afinal.intelligencetrash.ui.dialog.WaitingDialog;
import com.afinal.intelligencetrash.ui.interfaces.LoginView;
import com.afinal.intelligencetrash.ui.interfaces.RegisterView;
import com.afinal.intelligencetrash.utils.SimpleUtil;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by cyy on 17-10-7.
 */

public class LoginActivity extends AbstractBaseToolbarCoreActivity implements LoginView {

    @BindView(R.id.et_username) AppCompatEditText et_username;
    @BindView(R.id.et_pwd) AppCompatEditText et_pwd;
    @BindView(R.id.tv_register) AppCompatTextView tv_register;
    @BindView(R.id.bu_login) AppCompatButton bu_login;
    private WaitingDialog dialog;
    @Inject
    LoginServiceImp service;


    @Override
    protected void initView() {
        //依赖注入
        ButterKnife.bind(this);
        ActivityModule.getInstance().addActivity(getClass(), this);
        ((CustomApplication)getApplication()).getActivityComponent().inject(this);

        setToolbarHide(true);
        bu_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = et_username.getText().toString().trim();
                String pwd = et_pwd.getText().toString().trim();
                if("".equals(username) || "".equals(pwd)){
                    SimpleUtil.showToast(LoginActivity.this, "用户名或密码为空!");
                    return;
                }
                service.login(username, pwd, new ActionCallBack() {
                    @Override
                    public void before() {
                        OnLogining();
                    }

                    @Override
                    public void after(final boolean isSuccess) {
                        int delay = SimpleUtil.generateRadomNum(500, 2000);
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                OnLoginEnd(isSuccess);
                            }
                        }, delay);
                    }
                });
            }
        });
        String text = getResources().getString(R.string.toRegister);
        SpannableString spannableStr = new SpannableString(text);

        spannableStr.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                RegisterActivity.launchActivityForResult(LoginActivity.this, RegisterActivity.REGISTER_REQUEST_CODE);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(getResources().getColor(R.color.button_normal));
                ds.setUnderlineText(false);
            }
        }, 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv_register.setMovementMethod(LinkMovementMethod.getInstance());
        tv_register.setText(spannableStr);
    }

    private void OnLogining(){
        showWiatingDalig("登录中...");
    }

    private void OnLoginEnd(boolean isSuccess){
        dismissDialog();
        if(isSuccess){
            MainActivity.launchActivity(this);
            finish();
        }
    }
    @Override
    public void showWiatingDalig(String text){
        if(dialog == null){
            dialog = new WaitingDialog.Builder()
                    .setCancelable(true)
                    .build(LoginActivity.this);
        }
        dialog.setTextView(text);
        dialog.show();
    }
    @Override
    public void dismissDialog(){
        if(dialog != null){
            dialog.dismiss();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case RegisterView.REGISTER_REQUEST_CODE:{
                if(resultCode == RegisterView.REGISTER_SUCCESS){
                    String username = data.getExtras().getString("username");
                    String pwd = data.getExtras().getString("pwd");
                    et_username.setText(username);
                    et_pwd.setText(pwd);
                }
            } break;
        }
    }

    public static void launchActivity(Context context){
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void showToast(String text) {
        SimpleUtil.showToast(this, text);
    }


    @Override
    protected void initData() {
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_login;
    }

    @Override
    protected String getToolbarTitle() {
        return "登录";
    }

    @Override
    protected boolean isDisplayHomeAsUpEnabled() {
        return false;
    }

    @Override
    protected void dealloc() {
        dialog = null;
    }


}
